package top.go2do.common.resolver;

import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import top.go2do.common.annotation.UserInfoFill;
import top.go2do.common.module.UserInfoBO;
import top.go2do.common.threadLocal.UserInfoContext;

/**
 * @author xingyuji
 * @date 2025/3/27 06:32
 * @description
 */
@Component
public class UserInfoArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(UserInfoFill.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        if (parameter.getParameterType().equals(UserInfoBO.class)) {
            UserInfoBO userInfoBO = UserInfoContext.get();
            return userInfoBO;
        }
        // 无法处理时抛出异常
        throw new IllegalArgumentException("Unsupported parameter type");
    }
}
